"""
各种基础容器类
1、一维：List、Tree
3、二维：Table
"""

import sys
from typing import Optional
from PySide6.QtCore import QModelIndex
from PySide6.QtWidgets import (QApplication, QHBoxLayout, QHeaderView, QListWidget, QListWidgetItem, QSplitter,
                               QTableWidget, QTableWidgetItem, QTreeWidget, QTreeWidgetItem, QWidget)


class ContainerWidget(QWidget):
    def __init__(self, parent: Optional[QWidget] = None) -> None:
        super().__init__(parent=parent)
        self.__init_subwidget()
        self.__init_connect()
        self.__init_layout()

    def __init_subwidget(self):
        self.list = QListWidget()
        self.__init_list()
        self.tree = QTreeWidget()
        self.__init_tree()
        self.table = QTableWidget()
        self.__init_table()

    def __init_list(self):
        self.list.addItem("a")
        self.list.addItems(["b", "c", "d"])

    def __init_tree(self):
        self.tree.setColumnCount(3)
        self.tree.setHeaderLabels(["col1", "col2", "col3"])
        top_a = QTreeWidgetItem("a")
        sub_b = QTreeWidgetItem("b")
        sub_c = QTreeWidgetItem("c")
        sub_d = QTreeWidgetItem("d")

        top_a.addChild(sub_b)
        top_a.addChildren([sub_c, sub_d])
        self.tree.addTopLevelItem(top_a)
        self.tree.expandAll()

    def __init_table(self):
        self.table.setColumnCount(3)
        self.table.setHorizontalHeaderLabels(["col1", "col2", "col3"])
        self.table.setRowCount(4)
        self.table.setItem(0, 0, QTableWidgetItem("a"))
        self.table.setItem(0, 1, QTableWidgetItem("b"))
        self.table.setItem(1, 0, QTableWidgetItem("c"))
        self.table.setItem(1, 1, QTableWidgetItem("d"))
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.table.setSortingEnabled(True)

    def __init_connect(self):
        self.list.itemDoubleClicked.connect(self.__slot_item_double_clicked)
        self.tree.itemDoubleClicked.connect(self.__slot_item_double_clicked)
        self.table.doubleClicked.connect(self.__slot_item_double_clicked)
        # itemDoubleClicked会被doubleClicked覆盖
        self.table.itemDoubleClicked.connect(self.__slot_item_double_clicked)

    def __slot_item_double_clicked(self, item):
        if isinstance(item, QTableWidgetItem):
            # 表格类的存在的元素被双击
            self.setWindowTitle(
                f"{item.row()}, {item.column()}, " + item.text())
        elif isinstance(item, QModelIndex):
            # 表格类暂未添加的元素
            self.setWindowTitle(f"{item.row()}, {item.column()}, ")
        elif isinstance(item, QTreeWidgetItem):
            self.setWindowTitle(str(item.columnCount()) +
                                ", col0 = " + item.text(0))
        elif isinstance(item, QListWidgetItem):
            self.setWindowTitle(item.text())

    def __init_layout(self):
        hsplit = QSplitter()
        hsplit.addWidget(self.list)
        hsplit.addWidget(self.tree)
        hsplit.addWidget(self.table)

        hbox = QHBoxLayout()
        hbox.addWidget(hsplit)
        self.setLayout(hbox)


if __name__ == "__main__":
    # 主流程控制，一个程序只能有一个QApplication，sys可以用于命令行导入
    app = QApplication(sys.argv)
    window = ContainerWidget()
    window.show()
    sys.exit(app.exec())               # 控制程序安全退出
